import ToastComponent from './index.vue';

let $vm;

export default {
    install (vue) {
        if (!$vm) {
            const Toast = vue.extend(ToastComponent)
            $vm = new Toast({
                el: document.createElement('div')
            });
            document.body.appendChild($vm.$el);
        }

        const toast = {
            show (options) {
                options = typeof options === 'object'? options: {msg: options}; 
                options.msg = options.msg? options.msg: '';
                options.type = options.type? options.type: '';
                options.duration = options.duration? options.duration: 1500;
                options.location = options.location? options.location: 'middle'; 
                options.cb = options.cb? options.cb: null
                for (let i in options) {
                    if (i !== 'content') {
                        $vm[i] = options[i]
                    } else {
                        $vm.$el.querySelector('.aui-toast__inner__content__body').innerHTML = options['content']
                    }
                }

                $vm.onShow($vm);
            },
            hide () {
                $vm.show = !1;
            }
        }

        if (!vue.$aui) {
            vue.$aui = {
                toast
            }
        } else {
            vue.$aui.toast = toast
        }

        vue.mixin({
            created: function () {
                this.$aui = vue.$aui
            }
        });
    }
}
